package com.ruoyi.project.cms.type.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.cms.commons.CMSConst;
import com.ruoyi.project.cms.type.domain.Type;
import com.ruoyi.project.cms.type.service.IAddTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/cms/type")
public class AddTypeController extends BaseController {
    private String prifex="cms/type";

    @Autowired
    private IAddTypeService addTypeService;

    /**
     * 新增类型
     */
    @GetMapping("/add")
    public String add() {
        return prifex + "/addType";
    }


    /**
     * 新增保存类型
     */
    @RequiresPermissions("cms:type:add")
    @Log(title = "类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Type type) {
        if (CMSConst.TYPE_NAME_NOT_UNIQUE.equals(addTypeService.checkTypeNameUnique(type))) {
            return error("新增类型'" + type.getTypeName() + "'失败，类型名称已存在");
        }

        return toAjax(addTypeService.insertType(type));
    }

    /**
     * 检查公告类型名称是否唯一
     *
     * @return 字符串0 - 唯一数据，可以使用
     * 字符串1 - 非唯一数据，不可使用
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(Type type) {
        return addTypeService.checkTypeNameUnique(type);
    }
}